<?php

namespace app\admin\model\xilumarket\product;

use app\admin\model\xilumarket\user\User;
use app\common\model\xilumarket\Area;
use think\Model;
use traits\model\SoftDelete;

class Product extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'xilumarket_product';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'status_text',
        'sale_out_status_text',
        'freight_status_text',
        'sale_out_time_text'
    ];
    

    protected static function init()
    {
        self::beforeWrite(function ($row){
            $change = $row->getChangedData();
            if(isset($change['province_id']) || isset($change['city_id']) || isset($change['region_id']) || isset($change['address'])){
                $province  = Area::getNameById($row['province_id']);
                $city  = Area::getNameById($row['city_id']);
                $region  = Area::getNameById($row['region_id']);
                $geo = xilumarketGetGeo($province.$city.$region.trim($row['address']));
                if($geo['status']){
                    $row['lat']  = $geo['lat'];
                    $row['lng']  = $geo['lng'];
                }
                return $row;
            }
        });

        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2')];
    }

    public function getCheckStatusList(){
        return ['1' => __('Check_status 1'), '2' => __('Check_status 2'), '3' => __('Check_status 3')];
    }

    public function getSaleOutStatusList()
    {
        return ['0' => __('Sale_out_status 0'), '1' => __('Sale_out_status 1'), '2' => __('Sale_out_status 2')];
    }

    public function getFreightStatusList()
    {
        return ['1' => __('Freight_status 1'), '2' => __('Freight_status 2'), '3' => __('Freight_status 3')];
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getSaleOutStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['sale_out_status']) ? $data['sale_out_status'] : '');
        $list = $this->getSaleOutStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getFreightStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['freight_status']) ? $data['freight_status'] : '');
        $list = $this->getFreightStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getSaleOutTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['sale_out_time']) ? $data['sale_out_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function user(){
        return $this->belongsTo(User::class,'user_id','id',[],'left')->setEagerlyType(0);
    }
    public function category(){
        return $this->belongsTo(ProductCategory::class,'category_id','id',[],'left')->setEagerlyType(0);
    }
}
